//
//  WHCategoryScrollView.m
//  GroupFourFM
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WHCategoryScrollView.h"

@implementation WHCategoryScrollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        [self createSubViews];
    }
    
    return self;
}

- (void)createSubViews{
    
        
    self.isRight = NO;
    self.currentIndex = 0;
    self.btnArray = [NSMutableArray array];
    //标题小scroll
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CURRENTSCEEN.size.width, 40*HEI)];
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    self.titleScrollView.bounces = NO;
    self.titleScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.titleScrollView];
    
    //下划线 小方框
    self.vernier = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*0.03, self.titleScrollView.frame.size.height - 2, 50*WID, 2)];
    self.vernier.backgroundColor = COLOR_SELECTED;
    [self.titleScrollView addSubview:self.vernier];
    
    
    //大的背景
    self.bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40*HEI, CURRENTSCEEN.size.width, CURRENTSCEEN.size.height - 40*HEI)];
    //self.bgScrollView.backgroundColor = [UIColor lightGrayColor];
    self.bgScrollView.showsHorizontalScrollIndicator = NO;
    self.bgScrollView.bounces = NO;
    self.bgScrollView.delegate = self;
    self.bgScrollView.pagingEnabled = YES;
    [self addSubview:self.bgScrollView];
    
}
//将title传进数组里
- (void)setTitleContent:(NSArray *)array{
    
    CGFloat btnWidth = 0.0;
    NSInteger i = 0;
    
    //根据传进来的数组创建按钮
    for (NSString *title in array) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(SCREEN_WIDTH*0.03 + btnWidth, 5*HEI, 20, self.titleScrollView.frame.size.height);
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.titleScrollView addSubview:btn];
        
        [btn sizeToFit];

        
        [self.btnArray addObject:btn];
        NSLog(@"%ld",btn.titleLabel.text.length);
        
        btnWidth += btn.frame.size.width+SCREEN_WIDTH*0.03;
        i++;
    }
    
    UIButton * button = (UIButton *)[self viewWithTag:100];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.vernier.frame = CGRectMake(button.frame.origin.x, self.titleScrollView.frame.size.height-2, button.frame.size.width, 2);
    self.titleScrollView.contentSize = CGSizeMake(btnWidth+SCREEN_WIDTH*0.03, 0);
    
}


#pragma mark - 将tableView放进数组传进来加到大背景上
- (void)setBgScrollViewContents:(NSArray *)array{
    
    if (array.count != self.titleScrollView.subviews.count - 1) {
        NSLog(@"titleScrollView上的button个数与bgScrollview所对应的个数不等!!!");
    }
    
    for (int i = 0; i < array.count; i++) {
        UITableView *tableView = [array objectAtIndex:i];
        tableView.frame = CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, HEIGHT_NOBAR);
        [self.bgScrollView addSubview:tableView];
    }
    
    self.bgScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * array.count, 0);
    
}

#pragma mark - 点击btn 改变大的背景偏移量和小框框
-(void)btnAction:(UIButton *)btn{
    
    
    CGFloat w = CURRENTSCEEN.size.width;
    self.bgScrollView.contentOffset = CGPointMake(w * (btn.tag - 100), 0);
    
    [UIView animateWithDuration:0.5 animations:^{
        
         self.vernier.frame = CGRectMake(btn.frame.origin.x, self.titleScrollView.frame.size.height-2, btn.frame.size.width, 2);
        
    } completion:^(BOOL finished) {
        
    }];
    
    for (UIButton *button in self.btnArray) {
        
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
#pragma mark - ***********协议方法传值
    [self.WHCategorydelegate titleButtonAction:btn];
}
#pragma mark -scrollView协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (self.currenX > scrollView.contentOffset.x) {
        self.isRight = NO;
    }else{
        self.isRight = YES;
    }
    
    [UIView animateWithDuration:0.1 animations:^{
        
        NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
        UIButton * button = (UIButton *)[self viewWithTag:index+100];
        self.vernier.frame = CGRectMake(button.frame.origin.x, self.titleScrollView.frame.size.height-2, button.frame.size.width, 2);
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
            CGPoint point = CGPointMake(0, 0);
            
            //让当前页是和标题页对应
            if (self.currentIndex != index) {
                self.currentIndex = index;
                
                [self.WHCategorydelegate endDraggingAction:self.currentIndex];
            }
            
            if (index >= 3 && index < self.btnArray.count - 2) {
                
                UIButton *button = (UIButton *)[self viewWithTag:100];
                NSInteger btnCount = SCREEN_WIDTH / (button.frame.size.width + button.frame.origin.x);
                //NSLog(@"%ld", btnCount);
                point.x = CURRENTSCEEN.size.width / btnCount * (index - 2);
                
                self.titleScrollView.contentOffset = point;
            }else if (index < 3){
                self.titleScrollView.contentOffset = point;
            }
            
            if (self.titleScrollView.contentOffset.x <= 0) {
                point.x = 0;
                self.titleScrollView.contentOffset = point;
            }else if (self.titleScrollView.contentOffset.x >= self.titleScrollView.contentSize.width){
                point.x = self.titleScrollView.contentSize.width;
                self.titleScrollView.contentOffset = point;
            }
            
        }];
        
    }];
    
    self.currenX = scrollView.contentOffset.x;
    
    
    
}
#pragma mark - scrollView停止拖拽时
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    self.currenX = self.vernier.frame.origin.x;
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    for (int i = 0; i < self.btnArray.count; i++) {
        
        UIButton *button = [self.btnArray objectAtIndex:i];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    UIButton *button = [self.btnArray objectAtIndex:self.bgScrollView.contentOffset.x/(375*WID)];
    
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}


@end
